<?php
namespace app\controllers\h5;

use app\controllers\h5\base\H5Controller;
use app\models\product\ProductArea;
use app\models\product\ProductArea as Model;
use app\models\product\Product;
use app\models\product\ProductAreaList;
use app\models\product\ProductCategory;
use app\redis\Cache;

/**
 * Class AreaController
 * @package app\controllers\h5
 */
class AreaController extends H5Controller
{
    /**
     * Area list
     *
     * @return array|string[]|\yii\web\Response
     */
    public function actionList()
    {
        // area
        $id = intval($this->post('id'));

        $key = Cache::$AREA . $id;
        $cache = Cache::get($key);
        if (!$cache || true) {
            $area = Model::find()
                ->where(['id' => $id])
                ->andWhere(['status' => Model::$statusMapping['OK']])
                ->one();

            if (!$area) {
                return $this->fail('此区不存在');
            }

            // title
            $title = $area->name;

            // product ids
            $product_ids = ProductAreaList::find()
                ->where(['area_id' => $id])
                ->select(['product_id'])
                ->column();

            // category ids
            $category_ids = Product::find()
                ->where(['id' => $product_ids])
                ->andWhere(['status' => Product::$statusMapping['OK']])
                ->select(['category_id'])
                ->column();

            // category list
            $category_list = ProductCategory::find()
                ->where(['id' => $category_ids])
                ->andWhere(['status' => ProductCategory::$statusMapping['OK']])
                ->with(['image'])
                ->orderBy(['sort' => SORT_ASC, 'id' => SORT_ASC])
                ->asArray()
                ->all();

            Cache::set($key, json_encode([
                'title' => $title,
                'product_ids' => $product_ids,
                'category_list' => $category_list
            ]));
        } else {
            $data = json_decode($cache, true);
            $title = $data['title'];
            $product_ids = $data['product_ids'];
            $category_list = $data['category_list'];
        }

        $page = intval($this->post('page', $this->page));
        $pageSize = intval($this->post('pageSize', $this->pageSize));

        $key = Cache::$AREA_PRODUCT . $id . '-' . $page;
        $cache = Cache::get($key);
        if (!$cache || true) {
            $query = Product::find()
                ->where(['status' => Model::$statusMapping['OK']])
                ->andWhere(['id' => $product_ids]);

            // 身份特别处理
            if ($id == ProductArea::$KEY['REPORT']) {
                // $query->andWhere(['is_recommend' => 1]);
            }

            $totalCount = $query->count();
            $totalPage = ceil($totalCount / $pageSize);

            $list = $query->orderBy(['sort' => SORT_ASC, 'id' => SORT_DESC])
                ->offset(($page - 1) * $pageSize)
                ->limit($pageSize)
                ->with(['mainImage', 'scoreCost', 'area', 'area.name'])
                ->asArray()
                ->all();

            Cache::set($key, json_encode([
                'list' => $list,
                'totalCount' => $totalCount,
                'totalPage' => $totalPage
            ]));

        } else {
            $data = json_decode($cache, true);
            $list = $data['list'];
            $totalPage = $data['totalPage'];
            $totalCount = $data['totalCount'];
        }

        return $this->successEncode([
            'title' => $title,
            'category_list' => $category_list,
            $this->list => $list,
            $this->totalCount => (int)$totalCount,
            $this->totalPage => $totalPage,
            $this->currentPage => $page
        ]);
    }

}